//
//  PitchItemViewController.m
//  KG
//
//  Created by Eugene Braginets on 05.04.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PitchItemViewController.h"
#import "Place.h"
#import "AppDelegate.h"
#import "bannerView.h"

@implementation PitchItemViewController
@synthesize webView=_webView;
@synthesize activityIndicator=_activityIndicator;
@synthesize bannerViewController=_bannerViewController;
@synthesize placeID=_placeID;
@synthesize place=_place;

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

- (IBAction)checkinButton:(id)sender {
    [[myAppDelegate facebook] requestWithGraphPath:@"me/feed" 
                              andParams:[NSMutableDictionary dictionaryWithObject:@"157060520972164" forKey:@"place"]
                          andHttpMethod:@"POST"
                            andDelegate:myAppDelegate];
}

- (id)initWithPlace:(Place *)place {
    self = [super initWithNibName:@"PitchItemViewController" bundle:nil];
    if (self) {
        
        NSLog(@"Place: %@",place.placeID);
        self.placeID=place.placeID;
        
        NSString* url=[NSString stringWithFormat:@"%@&item=%@",kGetPitchesURL,self.placeID];
        NSString* name=[NSString stringWithFormat:@"%@%@",kPitchItem,self.placeID];
        [myAppDelegate getData:url andType:name];
        self.navigationItem.title=place.title;
        
//        NSString* name=[NSString stringWithFormat:@"%@%@",kPitchItem,self.placeID];
        
        //    NSLog(@"name: %@",[self.playerItem name]);
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector: @selector(reload:)
                                                     name:name 
                                                   object: nil];
        
        // Custom initialization
        
    }
    return self;
}


- (void) reload:(NSNotification*) notify {
    NSLog (@"reload %@%@",kPitchItem,self.placeID);
    
    self.place= [[[myAppDelegate dataArraysStore] objectForKey:[NSString stringWithFormat:@"%@%@",kPitchItem,self.placeID]] objectAtIndex:0];
    
    NSString* str;
//    NSLog(@"imageAddr: %@",self.place.imageAddr);
//    NSLog(@"Descr: %@",self.place.description);
    if (!self.place.description){
        self.place.description=@"";
    }
    
    str=[NSString stringWithFormat:@"<div style=\"float: left; clear: right; padding:6px;\"><IMG title=\"loading...\" src=\"%@\" width=\"290px\"></div><br/> %@ <br/>",self.place.imageAddr,self.place.description];
    [self.webView loadHTMLString:str baseURL:nil];
    
}



#pragma mark - View lifecycle

- (void)viewDidLoad
{
//    UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
//                                     initWithTitle:@"Check-In"                                            
//                                     style:UIBarButtonItemStyleBordered 
//                                     target:self 
//                                     action:@selector(checkIn)];
//    self.navigationItem.rightBarButtonItem = logoutButton;
//    [self.view setBackgroundColor:[UIColor colorWithRed:.3 green:.6 blue:.1 alpha:1]];
    
    [self.webView setHidden:YES];
    [self.webView setBackgroundColor:[UIColor clearColor]];
    [self.webView setOpaque:NO];
    
    [self.activityIndicator setHidden:NO];
    [self.activityIndicator startAnimating];
    
    bannerView *bannerVC=[[bannerView alloc] init];
    [self.view addSubview:bannerVC.view];
    [bannerVC animate];
    self.bannerViewController=bannerVC;
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setWebView:nil];
    [self setActivityIndicator : nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:[NSString stringWithFormat:@"%@%@",kPitchItem,self.placeID]
     object:nil];
    [self.webView stopLoading];
    self.webView.delegate = nil;
    
    [super viewWillDisappear:animated];
    
}


#pragma mark - WebView methods

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    //  self.careerWebView.frame = CGRectMake(320, 10, 315, 350);
    self.webView.hidden = NO;
    self.webView.alpha=0;
    [UIView animateWithDuration:0.4 animations:^{
        //        self.careerWebView.frame = CGRectMake(0, 10, 315, 350);
        self.webView.alpha=1;
    }];
    
    [self.activityIndicator setHidden:YES]; 
    
    
}
                                                      
#pragma mark - facebook feature
                                                      
- (void) checkIn {
  
  
  
}

#pragma mark - Other View Methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

@end
